﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using System.Device.Location;

namespace ScannerDemo
{

    public partial class Bill : PhoneApplicationPage
    {
        App App = App.Current as App;
        WebClient client;
        GeoCoordinateWatcher watcher;
        string Latitude, Longtitude,Location;
        int itemNumber;
        public Bill()
        {
            itemNumber = App.GlobalShopList.Count;
            /// PRICE CHECK
            App.totalPrice = 0;
            for (int i = 0; i < itemNumber; i++)
            {
                App.totalPrice = App.totalPrice + (float.Parse(App.GlobalShopList[i].Prices) * float.Parse(App.GlobalShopList[i].Quantity));
            }
            InitializeComponent();
            if (App.totalPrice > 100)
            {
                Service.Text = "0 $";
                total.Text = (App.totalPrice + 0).ToString() + " $";
            }
            else if (App.totalPrice == 0)
            {
                Service.Text = "0 $";
                total.Text = "0 $";
                MessageBox.Show("No item in Cart");
            }
            else
            {
                total.Text = (App.totalPrice + 20).ToString() + " $";
                Service.Text = "20 $";
            }
            CashAmount.Text = App.totalPrice.ToString()+" $";
            StartLocationService(GeoPositionAccuracy.High);
        }


        /// <summary>
        ///     Sent Purchasing list to Database
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (App.totalPrice == 0)
            {
                MessageBox.Show("No item in Cart");
            }
            else
            {
                
                String OrderList = "";
                for (int i = 0; i < itemNumber; i++)
                {
                    OrderList = OrderList + App.GlobalShopList[i].Id + "-" + App.GlobalShopList[i].Quantity + "|";
                }
                client = new WebClient();
                string url = "http://localhost/Mobile/buy.php?id=" + App.LoginId + "&list=" + OrderList + "&total=" + App.totalPrice + "&location=" + Location;
                //string url = "http://192.168.1.105/Mobile/buy.php?id=" + App.LoginId + "&list=" + OrderList + "&total=" + App.totalPrice + "&location="+Location;
                client.OpenReadAsync(new Uri(url));
                client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
            }
            
        }
        /// <summary>
        /// SHOW PURCHASE STATUS
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                MessageBox.Show("Purchase Completed");
                App.GlobalShopList.Clear();
                App.totalPrice = 0;
                NavigationService.Navigate(new Uri("/Panorama.xaml", UriKind.Relative));
            }
            else
            {
                MessageBox.Show("Internet Connection Problems");
            }
        }
        /// <summary>
        /// Orientation Change
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            base.OnOrientationChanged(e);
        }
        /// <summary>
        /// LOCATION SERVICE
        /// </summary>
        /// <param name="accuracy"></param>
        private void StartLocationService(GeoPositionAccuracy accuracy)
        {

            watcher = new GeoCoordinateWatcher(accuracy);
            watcher.MovementThreshold = 20;
         
            watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);

            watcher.Start();
        }
        /// <summary>
        /// POSITION CHANGE EVENT
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            Latitude = e.Position.Location.Latitude.ToString("0.000");
            Longtitude = e.Position.Location.Longitude.ToString("0.000");
            Location = Latitude + "," + Longtitude;
            
        }




    }
}